# 会员注册登录管理模块的接口对象层
from utils.get_keyword import GetKeyword
from utils.send_method import SendMethod


class MemberInterface:
    def __init__(self):
        self.url = 'http://47.108.206.100:8085'

    # 发送验证码
    def get_auth_code(self, telephone):
        method = 'get'
        url = self.url + '/sso/getAuthCode'
        payload = {
            'telephone': telephone
        }
        return SendMethod.send_method(method, url, params=payload)

    # 提取验证码
    def get_verify_code(self, telephone):
        response = self.get_auth_code(telephone)
        return GetKeyword.get_keyword(response, 'data')

    # 会员注册
    def register(self, body):
        method = 'post'
        url = self.url + '/sso/register'
        return SendMethod.send_method(method, url, data=body)

    # 会员登录
    def login(self, body):
        method = 'post'
        url = self.url + '/sso/login'
        return SendMethod.send_method(method, url, data=body)

    # 提取令牌
    def get_token(self, body):
        resp = self.login(body)
        token = GetKeyword.get_keyword(resp, 'token')
        return {'Authorization': f'Bearer {token}'}

    # 查看会员信息接口(需要登录拿到用户令牌)
    def get_user_info(self, body):
        # 请求查看会员信息接口
        method = 'get'
        url = self.url + '/sso/info'
        # 提取令牌用于获取用户信息请求头中
        token_response = self.get_token(body)
        return SendMethod.send_method(method=method, url=url, headers=token_response, params=body['username'])

    # 更新密码(作业)[不需要token,短信验证码就已经认证了身份]
    def update_pwd(self):
        pass


if __name__ == '__main__':
    member = MemberInterface()
    # 注册
    telephone = '15572129211'
    # body = {
    #     'authCode': member.get_verify_code(telephone),
    #     'password': '123456',
    #     'telephone': telephone,
    #     'username': 'liyimei'
    # }
    # print(member.register(body))
    # 登录
    login_body = {
        'username': 'liyimei',
        'password': '123456'
    }
    # print(member.login(login_body))
    # 提取令牌
    # print(member.get_token(login_body))
    body = {
        'username': 'liyimei',
        'password': 123456
    }
    print(member.get_user_info(body))  # 调用查看会员信息